package com.middlegroup.payadmin.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.middlegroup.common.constants.MethodConstants;
import com.middlegroup.common.model.TransRequest;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.payadmin.mapper.SubjectMapper;
import com.middlegroup.payadmin.pojo.dto.DataGrid;
import com.middlegroup.payadmin.pojo.dto.SubjectDto;
import com.middlegroup.payadmin.pojo.po.Subject;
import com.middlegroup.payadmin.pojo.po.SystemCfg;
import com.middlegroup.payadmin.service.SubjectService;
import com.middlegroup.payadmin.service.SystemCfgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 主体表 服务实现类
 * </p>
 *
 * @author pc123
 * @since 2019-01-04
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Resource
    private SubjectMapper subjectMapper;

    @Resource
    private SystemCfgService systemCfgService;

    @Override
    public PageInfo<SubjectDto> listSubjectByName(DataGrid grid, String name,String code) {
        grid.getOrderBy();
        return PageHelper.startPage(grid.getPageNum(), grid.getPageSize())
                .doSelectPageInfo(() -> this.subjectMapper.listSubjectByName(name,code));
    }

    @Override
    public PageInfo<SystemCfg> getConfigs(String code) {
        TransRequest request = new TransRequest();
        request.setSubjectCode(code);
        TransResult transResult = systemCfgService.post(MethodConstants.GET_SUBJECT_CONFIGS,request);
        PageInfo<SystemCfg> pageInfo = new PageInfo<>();
        if (null != transResult){
            pageInfo.setList(transResult.getData());
        }
        return pageInfo;
    }

    @Override
    public Boolean removeSubjectConfig(String code,String category, String name) {
        TransRequest request = new TransRequest();
        request.setSubjectCode(code);
        TransResult transResult = systemCfgService.post(MethodConstants.REMOVE_SUBJECT_CONFIGS,request);
        if (null != transResult && transResult.getTransStatus().intValue() == 1){
            return true;
        }
        return false;
    }

    @Override
    public Boolean saveSubjectConfig(String code,String category, String name, String content, String remark) {
        TransRequest request = new TransRequest();
        request.setSubjectCode(code);
        TransResult transResult = systemCfgService.post(MethodConstants.PUT_SUBJECT_CONFIGS,request);
        if (null != transResult && transResult.getTransStatus().intValue() == 1){
            return true;
        }
        return false;
    }
}
